package com.swak.algs;

import java.util.LinkedList;
import java.util.Queue;

import com.swak.algs.node.TreeNode;

/**
 * 二叉树按层打印
 * 
 * @author lifeng
 */
public class Code11 {

	/**
	 * 按层打印树
	 * 
	 * @param head
	 */
	static void printTree(TreeNode head) {
		if (head == null) {
			return;
		}
		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(head);
		while (queue.size() > 0) {
			int size = queue.size();
			for (int i = 0; i < size; i++) {
				TreeNode node = queue.poll();
				System.out.print(node.val + " ");
				if (node.left != null) {
					queue.add(node.left);
				}
				if (node.right != null) {
					queue.add(node.right);
				}
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		TreeNode root = TreeNode.newBlanceTree();
		printTree(root);
	}
}
